//
//  NetWorkingTool.swift
//  sinaDemo
//
//  Created by mac on 15/12/19.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//

import AFNetworking
import SVProgressHUD

let domain = "com.baidu.data.error"

enum HTTPMethod: String {
    case GET = "GET"
    case POST = "POST"
}

class NetWorkingTool: AFHTTPSessionManager {
    
    // 单例对象
    static let shareNetworking: NetWorkingTool = {
        // baseurl
        let urlStr = "https://api.weibo.com/"
        let url = NSURL(string: urlStr)
        let tool = NetWorkingTool(baseURL: url)
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tool
    }()
    
    // 发送微博带图
    func requestWith(picture: NSData, urlString: String, parameters: [String : String]?, finished: (dict: [String : AnyObject]?, error: NSError?) ->()) {
        
        POST(urlString, parameters: parameters, constructingBodyWithBlock: { (formData) -> Void in
            /*
            参数的意思,第一个拼接的二进制数据,服务器接受上传文件的字段
            第二个参数 文件名
            第三个参数 文件的类型
            */
            formData.appendPartWithFileData(picture, name: "pic", fileName: "xxoo", mimeType: "image/jpeg")
            }, progress: { (progress) -> Void in
                
            }, success: { (_, result) -> Void in
                if let dict = result as? [String : AnyObject] {
                    finished(dict: dict, error: nil)
                }
                
            }) { (_, error) -> Void in
                finished(dict: nil, error: error)
        }
    }
    
    // 网络请求的核心方法封装
    func requestWith(HTTPType: HTTPMethod, urlString: String, parameters: [String : String]?, finished: (dict: [String : AnyObject]?, error: NSError?) ->()) {
        if HTTPType == HTTPMethod.POST {
            // 发送POST请求
            POST(urlString, parameters: parameters, progress: { (_) -> Void in
                
                }, success: { (_, result) -> Void in
                    // 在这里做字典转模型, 判断数据是否可以转换成字典
                    guard let dict = result as? [String : AnyObject] else {
                        let myError = NSError(domain: domain, code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据不合法"])
                        finished(dict: nil, error: myError)
                        return
                    }
                    
                    // 执行成功后的回调
                    finished(dict: dict, error: nil)
                }, failure: { (_, error) -> Void in
                    // 执行失败后的回调
                    finished(dict: nil, error: error)
            })
        }else {
            // GET请求
            GET(urlString, parameters: parameters, progress: { (_) -> Void in
                
                }, success: { (_, result) -> Void in
                    // 在这里做字典转模型, 判断数据是否可以转换成字典
                    guard let dict = result as? [String : AnyObject] else {
                        // domain: 反域名  com.baidu.error  code: 错误状态码 一般使用负数
                        // 错误信息
                        let myError = NSError(domain: domain, code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据不合法"])
                        finished(dict: nil, error: myError)
                        return
                    }
                    
                    // 执行成功后的回调
                    finished(dict: dict, error: nil)
                }, failure: { (_, error) -> Void in
                    // 执行失败后的回调
                    finished(dict: nil, error: error)
            })
        }
        
    }
}
